package map;

import java.util.HashMap;
import java.util.Map;

public class HashMapTest {

	public static void main(String[] args) {
		
		Student stu1 = new Student("1", "涂有", 23);
		Student stu2 = new Student("1", "涂有", 23);
		Student stu3 = new Student("1", "涂有", 23);
		
		Map<Student, Integer> map = new HashMap<Student, Integer>();
		count(map, stu1);
		count(map, stu2);
		count(map, stu3);
		
		System.out.println(map);
		
		System.out.println(stu1.hashCode());
		System.out.println(stu2.hashCode());
		System.out.println(stu1 == stu2);
		System.out.println(stu1.equals(stu2));
	}
	
	public static void count(Map<Student, Integer> map, Student stu){
		
		Integer count = map.get(stu);
		if(count == null){
			
			map.put(stu, 1);
		}else{
			
			map.put(stu, count+1);
		}
	}
}

class Student{
	
	private String id;
	private String name;
	private int age;
	
	public Student(String id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
